// 关于分类的小仓库
import { defineStore } from "pinia";
import { reqC1, reqC2, reqC3 } from "@/api/product/attr";
import { CategoryResponseData } from "@/api/product/attr/type.ts";
import { CategoryState } from "@/store/modules/types/type.ts";

const useCategoryStore = defineStore("categoryStore", {
  state: (): CategoryState => {
    return {
      // 一级分类列表
      c1Arr: [],
      // 存储选中的一级分类
      c1Id: "",
      // 二级分类列表
      c2Arr: [],
      // 存储选中的二级分类
      c2Id: "",
      // 三级分类列表
      c3Arr: [],
      //  存储选中的三级分类
      c3Id: "",
    };
  },
  actions: {
    // 获取一级分类列表
    async getC1() {
      let resultC1: CategoryResponseData = await reqC1();
      if (resultC1.code === 200) {
        this.c1Arr = resultC1.data;
      }
    },
    // 获取二级分类列表
    async getC2() {
      // 根据一级分类id获取二级分类
      let resultC2: CategoryResponseData = await reqC2(this.c1Id);
      if (resultC2.code === 200) {
        this.c2Arr = resultC2.data;
      }
    },
    async getC3() {
      // 根据二级分类id获取三级分类
      let resultC3: CategoryResponseData = await reqC3(this.c2Id);
      if (resultC3.code === 200) {
        this.c3Arr = resultC3.data;
      }
    },
  },
  getters: {},
});

export default useCategoryStore;
